package com.qinggeng.project.system.controller;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.Supplier;
import com.qinggeng.project.system.domain.SysLeague;
import com.qinggeng.project.system.service.ISupplierService;
import com.qinggeng.project.system.service.ISysLeagueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.jaas.AbstractJaasAuthenticationProvider;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供货商信息Controller
 *
 * @author smallfish
 * @date 2020-05-28
 */
@RestController
@RequestMapping("/system/supplier")
public class SupplierController extends BaseController
{
    @Autowired
    private ISupplierService supplierService;

    /**
     * 查询加盟信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier)
    {
        startPage();
        List<Supplier> supplierList = supplierService.listSupplier(supplier);
        return getDataTable(supplierList);
    }

    /**
     * 导出加盟信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:export')")
    @Log(title = "供货商信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Supplier supplier)
    {
        List<Supplier> supplierList = supplierService.listSupplier(supplier);
        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
        return util.exportExcel(supplierList, "supplier");
    }

    /**
     * 获取加盟信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(supplierService.getSupplierById(id));
    }

    /**
     * 修改加盟信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:edit')")
    @Log(title = "供货商信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supplier supplier)
    {
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除加盟信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:remove')")
    @Log(title = "供货商信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(supplierService.deleteSupplierByIds(ids));
    }


    /**
     * 处理供货商信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:edit')")
    @Log(title = "供货商信息", businessType = BusinessType.UPDATE)
    @PostMapping("/deal")
    public AjaxResult deal(@RequestBody Supplier supplier)    {
        if(!supplier.getStatus().equals(CommonEnum.STATUS_10.getValue())){
            return AjaxResult.error("信息已被处理过，请勿重复处理！",ShowTypeEnum.用户.getValue());
        }
        supplier.setStatus(CommonEnum.STATUS_20.getValue());
        return toAjax(supplierService.updateSupplier(supplier));
    }
}
